﻿using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.ServiceModel;
using TFile.ISA;

namespace TFile.CSA
{
    public partial class ServicoArquivo : ServiceBase
    {
        ServiceHost svh1;
        ServiceHost svh2;

        public ServicoArquivo()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            StartWCFService();
        }

        protected override void OnStop()
        {
            try
            {
                svh1.Close();
                svh2.Close();
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message);
            }
        }

        void StartWCFService()
        {
            try
            {
                NetTcpBinding ntcp = new NetTcpBinding();

                ntcp.MaxBufferPoolSize = 2147483647;
                ntcp.MaxReceivedMessageSize = 2147483647;
                ntcp.MaxBufferSize = 2147483647;
                ntcp.ReaderQuotas.MaxStringContentLength = 2147483647;
                ntcp.ReaderQuotas.MaxDepth = 2147483647;
                ntcp.ReaderQuotas.MaxBytesPerRead = 2147483647;
                ntcp.ReaderQuotas.MaxNameTableCharCount = 2147483647;
                ntcp.ReaderQuotas.MaxArrayLength = 2147483647;
                ntcp.SendTimeout = new TimeSpan(1, 10, 0);
                ntcp.ReceiveTimeout = new TimeSpan(1, 10, 0);

                svh1 = new ServiceHost(typeof(DocumentLibraryService));
                ((ServiceBehaviorAttribute)svh1.Description.Behaviors[0]).MaxItemsInObjectGraph = 2147483647;
                svh1.AddServiceEndpoint(typeof(IDocumentLibraryService), ntcp, "net.tcp://localhost:51975");
                svh1.Open();
                svh2 = new ServiceHost(typeof(XMLDataService));
                ((ServiceBehaviorAttribute)svh2.Description.Behaviors[0]).MaxItemsInObjectGraph = 2147483647;
                svh2.AddServiceEndpoint(typeof(IXMLDataService), ntcp, "net.tcp://localhost:51976");
                svh2.Open();
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message);
            }
        }
    }
}
